<?php

/**
 * 推送-审核箱： 
 *
 * @author mengyu wang
 */

require_once 'CommonController.php';

class Admin_AuditBoxController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_PushTask();
        $this->_userModel = new Application_Model_User();
        $this->view->title = "管理中心";
        
    } 
    
    /**
     * 总入口(只能查看)
     */
    public function pushListAction()
    {
        $this->view->key = $day = $this->_request->get('day');
        $this->view->flag = 0;
        $this->view->reportType = 'push-list';
        if ($day == '' || $day == 'today') {
            //默认为今天
            $this->opReportToday();
        } else if ($day == 'yestoday') {
            $this->opReportYestoday();
        } else if ($day == 'last-seven-day') {
            $this->opReportLastSevenDay();
        } else if ($day == 'last-thirty-day') {
            $this->opReportLastThirtyDay();            
        } else if ($day == 'last-three-month') {
            $this->opReportLastThreeMonth();
        } else if($day == 'all') {
            $this->opReportAll();
        } else if ($day == 'between') {
            $this->opReportBetween();
        }
        $this->renderScript('push-task/audit-box.php');
    }
    /**
     * 可以审核
     */
    public function pushListManageAction()
    {
        $this->view->key = $day = $this->_request->get('day');
        $this->view->flag = 1;
        $this->view->reportType = 'push-list-manage';
        if ($day == '' || $day == 'today') {
            //默认为今天
            $this->opReportToday();
        } else if ($day == 'yestoday') {
            $this->opReportYestoday();
        } else if ($day == 'last-seven-day') {
            $this->opReportLastSevenDay();
        } else if ($day == 'last-thirty-day') {
            $this->opReportLastThirtyDay();            
        } else if ($day == 'last-three-month') {
            $this->opReportLastThreeMonth();
        } else if($day == 'all') {
            $this->opReportAll();
        } else if ($day == 'between') {
            $this->opReportBetween();
        }
        $this->renderScript('push-task/audit-box.php');
    }
    
    /**
     * 草稿箱-(时间范围)
     * @param string $startTime
     * @param string $endTime
     * @param int $type
     */
    public function rangeMember($startTime, $endTime, $type = 1)
    {
        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();

        $this->view->title = '推送审核箱';
        $this->view->versions = $this->_model->getVersion($productCode, $startTime, $endTime,array(1,4));
        $this->view->osNames = $this->_model->getOs($productCode, $startTime, $endTime,array(1,4));
        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['productCode'] = $productCode;
        $vars['startTime'] = $startTime ? $startTime : trim($this->_request->get('startTime'));
        $vars['endTime'] = $endTime ? $endTime : trim($this->_request->get('endTime'));
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['version'] = trim($this->_request->get('version'));
        $vars['keyword'] = trim($this->_request->get('keyword'));
        $vars['day'] = trim($this->_request->get('day'));
        
        $urls = $vars;

        if ($vars['productCode'] != '') {
            $wheres[] = "`pp`.`productCode` = '{$vars['productCode']}'";
        }
        if ($vars['startTime'] != '' ) {
            $wheres[] = "`pp`.`startTime` >= '" . addslashes($vars['startTime'].' 0:0:0') . "'";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`pp`.`startTime` <= '" . addslashes($vars['endTime'].' 23:59:59') . "'";
        }
        if ($vars['osName'] != '') {
             $wheres[] = "`pp`.`osName` like '%" . addslashes($vars['osName']) . "%'";
        }
        if ($vars['version'] != '') {
            $wheres[] = "`pp`.`version` like '%" . addslashes($vars['version']) . "%'";
        }
        if ($vars['keyword'] != '') {
            $wheres[] = "`pp`.`msgInfo` like '%" . addslashes($vars['keyword']) . "%'";
        }
        //筛选出 待审核，审核不通过的
        $wheres[] = "`pp`.`status` = 1 or `pp`.`status` = 4";
        
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "
            SELECT `pp`.*, `u`.`name` AS `name`,`pp`.msgInfo,`pp`.startTime, `pp`.osName  FROM `{$this->_model->getTableName()}` AS `pp` 
            LEFT JOIN `{$this->_userModel ->getTableName()}` AS `u` ON `u`.`userName` = `pp`.`addUser`
            {$where} 
            ORDER BY `status` ASC,`pp`.`addTime` DESC
        ";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
    
    /**
     * 昨天
     * Enter description here ...
     * @param int type
     */
    public function opReportYestoday($type = 1)
    {
        $this->view->key = 'yestoday';
        $startTime = date('Y-m-d',strtotime("-1 day"));
        $this->rangeMember($startTime,$startTime,$type);    
    }    
     /**
     * 今天
     * Enter description here ...
     * @param int type
     */
    public function opReportToday($type = 1)
    {
        $this->view->key = 'today';
        $startTime = date('Y-m-d',strtotime("now"));
        $this->rangeMember($startTime,$startTime,$type);    
    }
    
    /**
     * 最近七天
     * Enter description here ...
     * @param int type
     */
    public function opReportLastSevenDay($type = 1)
    {
        $this->view->key = 'last-seven-day';
        $startTime = date('Y-m-d',strtotime("-8 days"));
        $endTime = date('Y-m-d',strtotime("-1 day"));        
        $this->rangeMember($startTime, $endTime, $type);
    }
    
    /**
     * 最近30天
     * Enter description here ...
     * @param int type
     */
    public function opReportLastThirtyDay($type = 1)
    {
        $this->view->key = 'last-thirty-day';
        $startTime = date('Y-m-d',strtotime("-31 days"));
        $endTime = date('Y-m-d',strtotime("-1 day"));        
        $this->rangeMember($startTime, $endTime, $type);
    }
    
    /**
     * 最近3个月
     * Enter description here ...
     * @param int type
     */
    public function opReportLastThreeMonth($type = 1)
    {
        $this->view->key = 'last-three-month';
        $startTime = date('Y-m-d',strtotime("-3 months"));
        $endTime = date('Y-m-d',strtotime("-1 day"));        
        $this->rangeMember($startTime, $endTime,$type);
    }
    
    /**
     * 选择时间段
     * Enter description here ...
     * @param int type
     */
    public function opReportBetween($type = 1)
    {
        $this->view->key = 'between';
        $this->view->title = '推送草稿箱';
        if ($this->_request->isPost() || 1) {
            $startTime = $this->_request->get('startTime');
            $endTime = $this->_request->get('endTime');
            $backUrl = $this->view->projectUrl(array('action' => 'push-list','day'=>'between','startTime'=>$startTime,'endTime'=>$endTime));
            if (!$startTime || !$endTime) {
                 echo $this->view->message('请选择时间范围！',$backUrl); exit;
            } else if($startTime>$endTime) {
                 echo $this->view->message('开始时间必须早于结束时间，请重新填写！', $backUrl); exit;  
            } else {
                $this->view->startTime = $startTime;
                $this->view->endTime = $endTime;
                @$this->view->vars['startTime'] = $this->view->startTime;
                @$this->view->vars['endTime'] = $this->view->startTime;
                
            }
            if ($startTime == $endTime) {
                $this->rangeMember($startTime, $startTime, $type);
            } else {
                $this->rangeMember($startTime, $endTime, $type);
            }
        }
    }
    
    /**
     * 拒绝发布（需要填写拒绝理由）
     */
    public function rejectAuditAction()
    {
    	$this->view->btnInfo = '拒绝发布';
    	$vars = array();
    	$id = $this->view->id = intval($this->_request->get('id'));
    	$this->view->row = $this->_model->fetchRow("id = {$id}");
        $this->view->startTime = $this->_request->get('startTime');
        $this->view->endTime = $this->_request->get('endTime');
        $this->view->osName = $this->_request->get('osName');
        $this->view->version = $this->_request->get('version');
        $this->view->keyword = $this->_request->get('keyword');
        //收集表单信息
        $filter = new Zend_Filter_StripTags();
        $vars['comments'] = $filter->filter(trim($this->_request->getPost('comments')));
        $vars['status'] = 4;
        if ($this->_request->isPost()) {
        	$this->_model->updateStatus($id, $vars['status'], $vars['comments']);
        	$rows = $this->_model->fetchRow("id = {$id}");
        	$this->_model->sendMail($rows);
	    	exit($this->view->message('已经拒绝发布消息！', 
                                    $this->view->projectUrl(array(
                                    'startTime'=>$this->_request->get('startTime'),
                                    'endTime'=>$this->_request->get('endTime'),
                                    'osName'=>$this->_request->get('osName'),
                                    'version'=>$this->_request->get('version'),
                                    'keyword'=>$this->_request->get('keyword'),
                                    'day'=>'between',
                                    'action'=>'push-list'))));
    	}
    	$this->renderScript('push-task/comment.php');
    }
    
    /**
     * 修改为审核箱中（状态改为1）
     */
    public function changeToAuditAction()
    {
    	$this->view->btnInfo = '更改为审核中';
    	$vars = array();
    	$id = $this->view->id = intval($this->_request->get('id'));
    	$this->view->row = $this->_model->fetchRow("id = {$id}");
        $this->view->startTime = $this->_request->get('startTime');
        $this->view->endTime = $this->_request->get('endTime');
        $this->view->osName = $this->_request->get('osName');
        $this->view->version = $this->_request->get('version');
        $this->view->keyword = $this->_request->get('keyword');
        //收集表单信息
        $filter = new Zend_Filter_StripTags();
        $vars['comments'] = $filter->filter(trim($this->_request->getPost('comments')));
        $vars['status'] = 1;
        if ($this->_request->isPost()) {
        	$this->_model->updateStatus($id, $vars['status'], $vars['comments']);
        	$rows = $this->_model->fetchRow("id = {$id}");
        	$this->_model->sendMail($rows);
	    	exit($this->view->message('已经将消息更改为审核中！', 
                                    $this->view->projectUrl(array(
                                    'startTime'=>$this->_request->get('startTime'),
                                    'endTime'=>$this->_request->get('endTime'),
                                    'osName'=>$this->_request->get('osName'),
                                    'version'=>$this->_request->get('version'),
                                    'keyword'=>$this->_request->get('keyword'),
                                    'day'=>'between',
                                    'action'=>'push-list'))));
    	}
    	$this->renderScript('push-task/comment.php');
    }
    
    /**
     * 审核通过
     */
    public function passAuditAction()
    {
    	$this->view->btnInfo = '审核通过';
    	$vars = array();
    	$id = $this->view->id = intval($this->_request->get('id'));
    	$this->view->row = $this->_model->fetchRow("id = {$id}");
        $this->view->startTime = $this->_request->get('startTime');
        $this->view->endTime = $this->_request->get('endTime');
        $this->view->osName = $this->_request->get('osName');
        $this->view->version = $this->_request->get('version');
        $this->view->keyword = $this->_request->get('keyword');
        //收集表单信息
        $filter = new Zend_Filter_StripTags();
        $vars['comments'] = $filter->filter(trim($this->_request->getPost('comments')));
        $vars['status'] = 3;
        if ($this->_request->isPost()) {
        	//加入PUSH队列表
        	$rows = $this->_model->fetchRow("id = {$id}");
        	$this->_model->addPush($rows);
        	$this->_model->updateStatus($id, $vars['status'], $vars['comments']); 
        	$rows = $this->_model->fetchRow("id = {$id}");
        	$this->_model->sendMail($rows);
	    	exit($this->view->message('相关PUSH信息已经通过审核！', 
                                    $this->view->projectUrl(array(
                                    'startTime'=>$this->_request->get('startTime'),
                                    'endTime'=>$this->_request->get('endTime'),
                                    'osName'=>$this->_request->get('osName'),
                                    'version'=>$this->_request->get('version'),
                                    'keyword'=>$this->_request->get('keyword'),
                                    'day'=>'between',
                                    'action'=>'push-list'))));
    	}
    	$this->renderScript('push-task/comment.php');
    }
    /**
     * 彻底删除指定的PUSH消息
     */
    public function deleteAuditAction()
    {
        $id = $this->_request->get('id');
        $this->_model->delete("id={$id}");
        exit($this->view->message('已经删除该消息！', 
                                    $this->view->projectUrl(array(
                                    'startTime'=>$this->_request->get('startTime'),
                                    'endTime'=>$this->_request->get('endTime'),
                                    'osName'=>$this->_request->get('osName'),
                                    'version'=>$this->_request->get('version'),
                                    'keyword'=>$this->_request->get('keyword'),
                                    'day'=>'between',
                                    'action'=>'push-list'))));
    }
    

}

